<?php

namespace Home\Controller;

use Think\Controller;

class AdminController extends Controller
{
    function adminLogin()
    {
        if (IS_AJAX) {
            $adminname = I('adminname');
            $adminpassword = I('adminpassword');
            $admin = D('admin');
            $ret1 = $admin->where(array('adminname' => $adminname))->find();
            if ($ret1) {
                if ($ret1['adminpassword'] == $adminpassword) {
                    $this->ajaxReturn(
                        array(
                            'info' => '1'
                        )
                    );
                } else {
                    $this->ajaxReturn(
                        array(
                            'info' => '用户名或密码不正确'
                        )
                    );
                }
            } else {
                $this->ajaxReturn(
                    array(
                        'info' => '用户名不存在'
                    )
                );
            }

        }

    }

    function toMain()
    {
        $adminname = I('adminname');
        $admin = D('admin');
        $ip = get_client_ip();
        $lastlogintime = 'current_timestamp()';
        $ret = $admin->where(array('adminname' => $adminname))->find();
        $lasttime = $ret['lastlogintime'];
        $lastip = $ret['lastloginip'];
        $sql = 'update yh_admin  set lastlogintime=' . $lastlogintime . ',lastloginip="'.$ip.'" where adminid=' . $ret['adminid'];
        $saveret = $admin->execute($sql);
        if ($saveret) {
            session('admin', $ret);
            $s=session('admin');
            $this->assign('lasttime', $lasttime);
            $this->assign('lastip', $lastip);
            $this->redirect('admin/main');
        } else {
           $this->error('系统出现错误  请稍后');
        }
    }
    function main(){
        $this->display('admin/main');
    }
    function checkIsLogin()
    {
        if (session('admin')) {
            return true;
        }
        $this->display('index/index');
    }
}
